// pages/searchPage/searchPage.js
//获取应用实例
const app = getApp()
const util = require('../../utils/util.js')
var that;
var searchKeyword = '';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    search_tagArray: wx.getStorageSync('tagArray') == null || wx.getStorageSync('tagArray') == '' ? [] : wx.getStorageSync('tagArray'),
    search_tag_hidden: false,
    datasource: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    that = this;
    util.setNavTitle('搜索')
    console.log(wx.getStorageSync('tagArray'))
    //获取屏幕宽度
    util.getWindowWidth(that);
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },
  //输入事件
  input: function(e) {
    if (e.detail.value.length == 0) {
      that.setData({
        search_tag_hidden: false
      })
    }
    searchKeyword = e.detail.value
  },
  //tag搜索
  tagSousuo: function(e) {
    searchKeyword = e.currentTarget.dataset.keyword;
    console.log(searchKeyword)
    that.setData({
      searchValue: searchKeyword
    })
    that.sousuo();
  },
  //搜索事件
  sousuo: function() {
    if (searchKeyword == '') {
      return;
    }
    var tagarray = that.data.search_tagArray;
      var is_save = true;
      for (var i=0;i<tagarray.length;i++) {
        if (tagarray[i].tag == searchKeyword) {
          is_save = false
          break;
        }
      }
      if (is_save) {
        tagarray.push({
          'tag': searchKeyword
        })
      }
      wx.setStorageSync('tagArray', tagarray)
      searchKeyword = '';
    that.setData({
      search_tagArray: tagarray,
      search_tag_hidden: true,
    })
    // util.getDataRequest('store/likeStore', {
    //   'storeName': searchKeyword,
    //   'community': wx.getStorageSync('xiaoqu_id')
    // }, function success(res) {

    //   var tagarray = that.data.search_tagArray;
    //   var is_save = true;
    //   for (var i=0;i<tagarray.length;i++) {
    //     if (tagarray[i].tag == searchKeyword) {
    //       is_save = false
    //       break;
    //     }
    //   }
    //   if (is_save) {
    //     tagarray.push({
    //       'tag': searchKeyword
    //     })
    //   }
    //   wx.setStorageSync('tagArray', tagarray)
    //   searchKeyword = '';
    //   that.setData({
    //     search_tagArray: tagarray,
    //     search_tag_hidden: true,
    //     datasource: res.Storelist
    //   })
    // }, function(res) {

    // })


  }
})